package com.qianduo.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.qianduo.service.impl.AccountServiceImpl;
import com.qianduo.struts.AbstractAction;
import com.qianduo.util.Common;
import com.qianduo.vo.AccountForm;
import common.page.PageList;
import common.page.Pagination;
import common.util.DateUtil;
import common.util.StringUtil;

public class AccountAction extends AbstractAction {	
	public ActionForward listAccount(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		//AccountForm accountForm			= (AccountForm) form;
		AccountServiceImpl service		= (AccountServiceImpl)getBean("accountService");
		PageList pageList				= Pagination.getPageList(req);
		List<AccountForm> list			= service.listAccount(null, null, null, null, null, null, null, null, null, null, null, null, null, null, pageList);
		req.setAttribute("list", list);
		return mapping.findForward("listAccount");
	}
	
	public ActionForward addAccount(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		
		return mapping.findForward("addAccount");
	}
	
	public ActionForward modify(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		AccountForm accountForm			= (AccountForm) form;
		AccountServiceImpl service		= (AccountServiceImpl)getBean("accountService");
		accountForm						= service.getQianduoAccount(accountForm.getUsername());
		req.setAttribute("accountForm", accountForm);
		return mapping.findForward("modifyAccount");
	}
	
	public ActionForward modifyAccount(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		List<String> errors			= new ArrayList<String>();
		AccountForm accountForm		= (AccountForm) form;
		if(StringUtil.isEmpty(accountForm.getUsername())){
			errors.add("用户名不能为空");
		}
		if(StringUtil.isEmpty(accountForm.getPassword())){
			accountForm.setPassword(StringUtil.encrypt("123456"));
		}else{
			accountForm.setPassword(StringUtil.encrypt(accountForm.getPassword()));
		}
		if(null == accountForm.getType() || accountForm.getType() == 0){
			errors.add("请选择帐号类型");
		}
		accountForm.setUpdateDate(DateUtil.getyyyy_MM_dd_HH_mm_ssString(DateUtil.getDate()));
		accountForm.setUpdateUser(Common.getSessionAccount(req));
		
		AccountServiceImpl service		= (AccountServiceImpl)getBean("accountService");
		if(errors.size() == 0){
			boolean ret	= service.saveOrUpdateAccount(accountForm);
			if(!ret){
				errors.add("添加用户出错！");
			}
		}
		if(errors.size() == 0){
			return listAccount(mapping, accountForm, req, res);
		}else{
			req.setAttribute("errors", errors);
			return modify(mapping, accountForm, req, res);
		}
	}
	
	public ActionForward saveOrUpdateAccount(ActionMapping mapping,ActionForm form,HttpServletRequest req,HttpServletResponse res){
		List<String> errors			= new ArrayList<String>();
		AccountForm accountForm		= (AccountForm) form;
		if(StringUtil.isEmpty(accountForm.getUsername())){
			errors.add("用户名不能为空");
		}
		if(StringUtil.isEmpty(accountForm.getPassword())){
			accountForm.setPassword(StringUtil.encrypt("123456"));
		}else{
			accountForm.setPassword(StringUtil.encrypt(accountForm.getPassword()));
		}
		if(null == accountForm.getType() || accountForm.getType() == 0){
			errors.add("请选择帐号类型");
		}
		accountForm.setCreateDate(DateUtil.getyyyy_MM_dd_HH_mm_ssString(DateUtil.getDate()));
		accountForm.setCreateUser(Common.getSessionAccount(req));
		
		AccountServiceImpl service		= (AccountServiceImpl)getBean("accountService");
		if(errors.size() == 0){
			AccountForm account			= service.getQianduoAccount(accountForm.getUsername());
			if(null != account){
				errors.add("您输入的用户名已存在");
			}			
		}
		if(errors.size() == 0){
			boolean ret	= service.saveOrUpdateAccount(accountForm);
			if(!ret){
				errors.add("添加用户出错！");
			}
		}
		if(errors.size() == 0){
			return listAccount(mapping, accountForm, req, res);
		}else{
			req.setAttribute("errors", errors);
			return addAccount(mapping, accountForm, req, res);
		}
	}
}